package com.example.xd.tuiliu_test.rtp.android.screenrecorder.rtp;


import java.io.IOException;

/**
 * Created by xmc on 2017/6/27.
 * com.android.screenrecorder.record
 */
public class RtpSenderWrapper {

    private RtpUdp mRtpUdp;
    private RtpAacStream mRtpAacStream;
    private RtpAvcStream mRtpAvcStream;

    public RtpSenderWrapper(String ip, int port, boolean broadcast) {
        mRtpUdp = new RtpUdp(ip, port, broadcast);

        if (mRtpUdp != null) {
            mRtpAvcStream = new RtpAvcStream(mRtpUdp);
            mRtpAacStream = new RtpAacStream(44100, mRtpUdp);
        }
    }

    /**
     * 发送视频
     *
     * @param data
     * @param offset
     * @param size
     * @param timeUs
     */
    public void sendAvcPacket(final byte[] data, final int offset, final int size, long timeUs) {
        if (mRtpAvcStream != null) {
            try {
                mRtpAvcStream.addPacket(data, offset, size, timeUs);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 发送音频
     *
     * @param data
     * @param offset
     * @param size
     * @param timeUs
     */
    public void sendAacPacket(final byte[] data, final int offset, final int size, long timeUs) {
        if (mRtpAacStream != null) {
            try {
                mRtpAacStream.addAU(data, offset, size, timeUs);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    //音视频同时关闭
    public void close() {
        if (mRtpUdp != null) {
            mRtpUdp.close();
        }
    }
}
